package com.hk.Mgr.web.controller.order;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.order.OrderInfoReq;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.vo.OrderExcelRes;
import com.hk.service.order.IContract;
import com.hk.service.order.IOrderContract;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/contract")
public class ContractCtrl extends BaseCtrl {

	@Autowired
	private IContract contractService;

	@Autowired
	private IOrderContract orderContractService;

	private Logger logger = LoggerFactory.getLogger(ContractCtrl.class);

	/**
	 * 获得所有订单信息
	 * 
	 * @author jeff
	 * @return
	 */
	@RequestMapping(value = "/get", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase<List<OrderExcelRes>> listOrder(
			@RequestBody OrderInfoReq req) {
		List<OrderExcelRes> orderExcellist = new ArrayList<OrderExcelRes>();
		ResponseBase<List<OrderExcelRes>> resp = new ResponseBase<>();

		if (req.getUserId() == null) {
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
			resp.setMsg("请登录后再尝试！");
			return resp;
		}

		String id = req.getId();
		String userId = req.getUserId().toString();
		String userName = req.getUserName();
		String beginDateStr = req.getBeginDate();
		String endDateStr = req.getEndDate();
		String financialName = req.getFinancialName();

		Integer curPage = req.getCurPage();
		Integer pageSize = req.getPageSize();

		Integer payStatus = req.getPayStatus();
		Integer company = req.getCompany();
		Long productId = req.getProductId();
		logger.info("order/list info:" + " userName =" + req.getUserName()
				+ " productId =" + req.getProductId() + " id = " + req.getId()
				+ " payStatus = " + req.getPayStatus() + " beginDate ="
				+ req.getBeginDate() + " endDate = " + req.getEndDate()
				+ " company=" + company + " financialName = " + financialName);


		Document doc = new Document();


		resp.setResultCode(StatusCode.SUCCESS.key());
		return resp;
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase<Boolean > create(@RequestBody OrderInfoReq req) throws IOException, DocumentException {
		Long orderId = Long.valueOf( req.getId());
		ResponseBase<Boolean> resp = new ResponseBase<>();

		contractService.create(orderId);

		resp.setResultCode(StatusCode.SUCCESS.key());
		resp.setResult(true);
		return  resp;
	}

    @RequestMapping(value = "/createOnePDF", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBase<String> genOne(Long orderId, Long contractId) throws IOException, DocumentException {

        ResponseBase<String> resp = new ResponseBase<>();

        String result = orderContractService.getPDFContarctUrlByOrder(orderId, contractId);

        resp.setResultCode(StatusCode.SUCCESS.key());
        resp.setResult(result);
        return resp;
    }

	@RequestMapping(value = "/reCreatePDFAndHTMLForSameContract", method = RequestMethod.GET)
	@ResponseBody
	public ResponseBase<HashMap> reCreatePDFAndHTMLForSameContract(Long contractId) throws IOException, DocumentException {

		ResponseBase<HashMap> resp = new ResponseBase<>();

        HashMap result = orderContractService.reCreatePDFAndHTMLForSameContract(contractId);

		resp.setResultCode(StatusCode.SUCCESS.key());
		resp.setResult(result);
		return resp;
	}
}
